package com.lianyu507.aop;

import com.lianyu507.annotation.DataArtical;
import com.lianyu507.task.LogTask;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;


/**
 * =================================
 * <p>
 * Created by Administrator on 2017/11/21.
 * <p>
 * 描述:
 */
@Aspect
@Component
public class LogAop {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Pointcut(value = "@annotation(com.lianyu507.annotation.DataArtical)")
    public void cutService() {

    }

    @Around("cutService()")
    public Object recordSysLog(ProceedingJoinPoint point) throws Throwable {
        //1.执行业务
        Object result = point.proceed();

        try {
            handle(point);
        } catch (Exception e) {
            log.error("日志记录出错!", e);
        }

        return result;
    }

    private void handle(ProceedingJoinPoint point) throws Exception {
        //获取拦截的方法名
        Signature signature = point.getSignature();
        MethodSignature methodSignature = null;

        if (!(signature instanceof MethodSignature)) {
            throw new IllegalArgumentException("该注解只能用于方法");
        }
        methodSignature = (MethodSignature) signature;
        Object target = point.getTarget();
        Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());

        //获取操作名称
        DataArtical annotation = currentMethod.getAnnotation(DataArtical.class);
        int type = annotation.type();
        int key = annotation.key();

        if (type == 1) {
            HttpServletRequest request = (HttpServletRequest) RequestContextHolder.getRequestAttributes();
            HashMap<String, String> values = new HashMap<>();
            Enumeration enums = request.getParameterNames();
            while (enums.hasMoreElements()) {
                String paramName = (String) enums.nextElement();
                String paramValue = request.getParameter(paramName);
                values.put(paramName, paramValue);
            }

            Iterator<Map.Entry<String, String>> iterator = values.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> en = iterator.next();
                String keyP = en.getKey();
                if ("id".equals(keyP)){
                    key = Integer.parseInt(en.getValue()); //拿到文章id
                    break;
                }

            }
        } else {
            log.error("统计类型不对");
        }

        LogTask.professionLog(type, key);
    }
}
